/**   
 *    
 * @file
 * @brief
 * @文件名	: ProtextViewEllipseEndFixed
 * @版权所有	: 网龙应用软件开发一部
 * @作  者	: chenqian
 * @创建时间	: 2014-8-18 上午10:15:07 
 * @文件描述	:  
 * @version	Ver 1.1
 */
package com.common.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.View;

import com.common.weibolib.ProTextView;

/**
 * fixed bug:ProTextView multiline & ellipsize="end" 中文不显示"..." <br>
 * Created 2014-8-18 上午10:15:07
 * 
 * @version 1.3
 * @author chenqian
 * 
 * @see
 */

public class ProtextViewEllipseEndFixed extends ProTextView {
	public static final String ELLIPSE_END = "…";
	protected int mELLIPSEWidth;
	protected int mMaxLines;
	protected CharSequence mOriText;
	protected SpannableStringBuilder mProccessText;
	protected boolean mSingleLine;
	protected static final boolean HAS_BUG = true;
	// Integer.parseInt(android.os.Build.VERSION.SDK) < 14; // <4.0
	protected boolean mChecked = !HAS_BUG;
	/** 全文view,满足在...后加入 全文或者更多 等提示 */
	protected View mTipView;

	/**
	 * Creates a new instance of ProtextViewEllipseEndFixed. <br>
	 * Created 2014-8-18 上午10:16:47
	 * 
	 * @param context
	 *            Context
	 */
	public ProtextViewEllipseEndFixed(Context context) {
		super(context);
	}

	/**
	 * Creates a new instance of ProtextViewEllipseEndFixed. <br>
	 * Created 2014-8-18 上午10:16:55
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 */
	public ProtextViewEllipseEndFixed(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * Creates a new instance of ProtextViewEllipseEndFixed. <br>
	 * Created 2014-8-18 上午10:17:05
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyle
	 *            int
	 */
	public ProtextViewEllipseEndFixed(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void setSingleLine(boolean singleLine) {
		mSingleLine = singleLine;
		super.setSingleLine(singleLine);
	}

	@Override
	public void setMaxLines(int maxlines) {
		mMaxLines = maxlines;
		mChecked = maxlines > 1 ? !HAS_BUG : true;
		super.setMaxLines(maxlines);
	}

	@Override
	public void setLineSpacing(float add, float mult) {
		mChecked = !HAS_BUG;
		super.setLineSpacing(add, mult);
	}

	/**
	 * 设置提示文字，满足在省略号后加入 “全文”“更多”等提示的需求
	 * 
	 * <br>
	 * Created 2014-10-14 下午2:12:09
	 * 
	 * @param tipView
	 *            提示控件
	 * @author chenqian
	 */
	public void setTipView(View tipView) {
		mTipView = tipView;
	}
	/**
	 * 显示提示控件
	 * 
	 * <br>Created 2014-10-14 下午2:16:45
	 * @author       chenqian
	 */
	public void showTipView(){
		if(null != mTipView){
			mTipView.setVisibility(View.VISIBLE);
		}
	}
	/**
	 * 隐藏提示控件
	 * 
	 * <br>Created 2014-10-14 下午2:17:17
	 * @author       chenqian
	 */
	public void hideTipView(){
		if(null != mTipView){
			mTipView.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onTextChanged(CharSequence text, int start, int before,
			int after) {
		mChecked = !HAS_BUG;
		super.onTextChanged(text, start, before, after);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mChecked = !HAS_BUG;
		super.onSizeChanged(w, h, oldw, oldh);
	}

	@Override
	public void setPadding(int left, int top, int right, int bottom) {
		mChecked = false;
		super.setPadding(left, top, right, bottom);
	}

	@Override
	public void setEllipsize(TruncateAt where) {
		if (HAS_BUG && where.equals(TruncateAt.END)) {
			mChecked = !HAS_BUG;
			if (mSingleLine) {
				setSingleLine(false);
			}
		} else {
			super.setEllipsize(where);
			mChecked = true;
		}
	}

	@Override
	public CharSequence getText() {
		if (mOriText != null) {
			return mOriText;
		}
		return super.getText();
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		if (HAS_BUG && !mChecked) {
			mChecked = true;
			Layout layout = super.getLayout();
			int maxLinex = mMaxLines > 0 ? mMaxLines : 1;
			if (layout.getLineCount() > maxLinex) {
				if (mELLIPSEWidth == 0) {
					mELLIPSEWidth = (int) getPaint().measureText(ELLIPSE_END);
				}
				mOriText = super.getText();
				int width = layout.getWidth(), llw = (int) layout
						.getLineWidth(maxLinex - 1), lci = layout
						.getLineEnd(maxLinex - 1);
				if (mELLIPSEWidth + llw > width) {
					int spc = mELLIPSEWidth + llw - width;
					int w = (int) (spc / super.getTextSize());
					if (spc % super.getTextSize() != 0) {
						++w;
					}
					lci -= w;
				}
				if(mOriText.charAt(lci - 1) == '\n'){
					lci--;
				}
				mProccessText = new SpannableStringBuilder(mOriText);
				mProccessText.delete(lci, mOriText.length())
						.append(ELLIPSE_END);
				// super.setText(mOriText.subSequence(0, lci) + ELLIPSE_END);
				super.setText(mProccessText);
				showTipView();
			}
		}
		super.onDraw(canvas);
	}

}
